package com.shensi.adminCollect.service.customer.impl;

import com.alibaba.fastjson.JSONObject;
import com.shensi.adminCollect.common.enums.WxConsumptionMessageEnum;
import com.shensi.adminCollect.domain.customer.ActivityMessagePushRecord;
import com.shensi.adminCollect.mapper.customer.ActivityMessagePushRecordMapper;
import com.shensi.adminCollect.service.customer.ActivityMessagePushRecordService;
import com.shensi.api.pay.client.RedisConsumptionThemeClient;
import com.shensi.common.core.domain.UniversalRedisMessageDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ActivityMessagePushRecord)表服务实现类
 *
 * @author makejava
 * @since 2023-09-20 16:05:28
 */


@Service("activityMessagePushRecordService")
public class ActivityMessagePushRecordServiceImpl implements ActivityMessagePushRecordService {
    @Resource
    private ActivityMessagePushRecordMapper activityMessagePushRecordMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${message.channel}")
    private String channel;

    @Autowired
    private RedisConsumptionThemeClient redisConsumptionThemeClient;
    private static final Logger log = LoggerFactory.getLogger(ActivityMessagePushRecordServiceImpl.class);
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ActivityMessagePushRecord queryById(Long id) {
        return this.activityMessagePushRecordMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param activityMessagePushRecord 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ActivityMessagePushRecord> queryByPage(ActivityMessagePushRecord activityMessagePushRecord, PageRequest pageRequest) {
        long total = this.activityMessagePushRecordMapper.count(activityMessagePushRecord);
        return new PageImpl<>(this.activityMessagePushRecordMapper.queryAllByLimit(activityMessagePushRecord, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param activityMessagePushRecord 实例对象
     * @return 实例对象
     */
    @Override
    public ActivityMessagePushRecord insert(ActivityMessagePushRecord activityMessagePushRecord) {
        this.activityMessagePushRecordMapper.insert(activityMessagePushRecord);
        return activityMessagePushRecord;
    }

    /**
     * 修改数据
     *
     * @param activityMessagePushRecord 实例对象
     * @return 实例对象
     */
    @Override
    public ActivityMessagePushRecord update(ActivityMessagePushRecord activityMessagePushRecord) {
        this.activityMessagePushRecordMapper.update(activityMessagePushRecord);
        return this.queryById(activityMessagePushRecord.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.activityMessagePushRecordMapper.deleteById(id) > 0;
    }

    @Override
    public Integer sendWxMessage(Long businessId, String businessName, String content, Integer sendFlag) {
        if(businessId!=null&& StringUtils.isNotEmpty(businessName)){
            ActivityMessagePushRecord pushRecord=new ActivityMessagePushRecord();
            pushRecord.setContent(content);
            pushRecord.setSendFlag(sendFlag);
            pushRecord.setBusinessId(businessId);
            pushRecord.setBusinessName(businessName);
            pushRecord.setStatus(0);
            if(sendFlag!=null&&sendFlag==1){
                try {
                    UniversalRedisMessageDto dto=new UniversalRedisMessageDto();
                    dto.setData(JSONObject.toJSONString(pushRecord));
                    dto.setMessageType(WxConsumptionMessageEnum.ACTIVITY_MESSAGE_PUSH.getValue());
                    dto.setTitle("ACTIVITY_MESSAGE_PUSH");
                    //String str=JSONObject.toJSONString(dto);
                    dto.setChannel(channel);
                    //redisTemplate.convertAndSend(channel, dto);
                    redisConsumptionThemeClient.publishMessageUse(dto);
                    //放进队列
                    pushRecord.setStatus(1);
                }catch (Exception e){
                    log.info("消息推送失败", e);
                }
            }
            this.insert(pushRecord);
        }
        return null;
    }

    public Integer sendWxMessage(Long businessId, String businessName){
        if(businessId!=null&& StringUtils.isNotEmpty(businessName)){
            ActivityMessagePushRecord q=new ActivityMessagePushRecord();
            q.setBusinessId(businessId);
            q.setBusinessName(businessName);
            List<ActivityMessagePushRecord> query = activityMessagePushRecordMapper.query(q);
            if(query.size()>0){
                ActivityMessagePushRecord record = query.get(0);
                if(record.getSendFlag()==1){
                    try {
                    UniversalRedisMessageDto dto=new UniversalRedisMessageDto();
                    dto.setData(JSONObject.toJSONString(record));
                    dto.setMessageType(WxConsumptionMessageEnum.ACTIVITY_MESSAGE_PUSH.getValue());
                    dto.setTitle("ACTIVITY_MESSAGE_PUSH");
                    dto.setChannel(channel);
                    //String str=JSONObject.toJSONString(dto);
                    //redisTemplate.convertAndSend(channel, dto);
                    redisConsumptionThemeClient.publishMessageUse(dto);
                        //放进队列
                        record.setStatus(1);
                    }catch (Exception e){
                        log.info("消息推送失败", e);
                    }
                    activityMessagePushRecordMapper.update(record);
                }
            }
        }


        return null;
    }

    @Override
    public Integer sendWxMessage(String channel, String jsonStr,WxConsumptionMessageEnum messageEnum) {
        if(channel!=null&&jsonStr!=null){
            UniversalRedisMessageDto dto=new UniversalRedisMessageDto();
            dto.setData(jsonStr);
            dto.setMessageType(messageEnum.getValue());
            dto.setTitle(messageEnum.getKey());
            dto.setChannel(channel);
            //String str=JSONObject.toJSONString(dto);
            //redisTemplate.convertAndSend(channel, dto);
            redisConsumptionThemeClient.publishMessageUse(dto);
        }

        return null;
    }

    @Override
    public List<ActivityMessagePushRecord> query(ActivityMessagePushRecord pushRecord) {
        return activityMessagePushRecordMapper.query(pushRecord);
    }
}
